package com.weacar.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weacar.R;
import com.weacar.bean.BrandBean;
import com.weacar.utils.CharacterParser;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SortAdapter extends BaseAdapter implements SectionIndexer {
    private List<BrandBean.MsgBean> list = null;
    private Activity mContext;
    private CharacterParser characterParser;
    private LayoutInflater mInflater;


    public SortAdapter(Activity mContext, List<BrandBean.MsgBean> list) {
        this.mContext = mContext;
        this.list = list;
        //实例化汉字转拼音类
        characterParser = CharacterParser.getInstance();
        mInflater = LayoutInflater.from(mContext);
    }


    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     */
    public void updateListView(List<BrandBean.MsgBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public int getCount() {
        return this.list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        final BrandBean.MsgBean brandBean = list.get(position);

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_lv_brand, viewGroup, false);
            viewHolder = new ViewHolder(convertView);
            // holder.mTextView = (TextView)convertView.findViewById(R.id.tv_title);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);

        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if (position == getPositionForSection(section)) {
            viewHolder.tv_index.setVisibility(View.VISIBLE);
            String pinyin = characterParser.getSelling(brandBean.getName());
            String headerText = pinyin.substring(0, 1).toUpperCase();
            if ("1".equals(headerText)) {
                headerText = "☆";
            }
            viewHolder.tv_index.setText(headerText);
        } else {
            viewHolder.tv_index.setVisibility(View.GONE);
        }

        viewHolder.tv_brand.setText(brandBean.getName());
        if (!brandBean.getIco().equals("")) {
            Glide.with(mContext).load(brandBean.getIco()).into(viewHolder.iv_icon);
        }
        return convertView;

    }


    static class ViewHolder {
        @BindView(R.id.tv_brand)
        TextView tv_brand;
        @BindView(R.id.iv_icon)
        ImageView iv_icon;
        @BindView(R.id.tv_index)
        TextView tv_index;


        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    public int getSectionForPosition(int position) {
        return list.get(position).getSortLetters().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = list.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    /**
     * 提取英文的首字母，非英文字母用#代替。
     *
     * @param str
     * @return
     */
    @SuppressWarnings("unused")
    private String getAlpha(String str) {
        String sortStr = str.trim().substring(0, 1).toUpperCase();
        // 正则表达式，判断首字母是否是英文字母
        if (sortStr.matches("[A-Z]")) {
            return sortStr;
        } else {
            return "#";
        }
    }

    @Override
    public Object[] getSections() {
        return null;
    }

}